﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;
using BOHVote.Common;
using BOHVote.Entity;
 

namespace BOH.DataManagement
{
    public partial class ApplyPeriodModfiy : System.Web.UI.Page
    {
        protected string PageType = string.Empty;
        protected string PeriodID = string.Empty;
        BHUser user = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            PageType = Request.QueryString["type"];
            PeriodID = Request.QueryString["ID"];
            if (!IsPostBack)
            {
                if (PageType != null && PeriodID != null && PageType == "edit" && PeriodID.Trim().Length != 0)
                {
                    FillForm(PeriodID);
                }
            }
            if (Session["User"] != null)
            {
                user = (BHUser)Session["User"];
            }
        }
        public void FillForm(string PeriodID)
        {
            DataSet dst = PeriodClass.getPeriodByID(PeriodID);
            if (dst.Tables[0].Rows.Count>0)
            {
                txtApplyPeriodID.Text = dst.Tables[0].Rows[0]["ApplyPeriodID"].ToString();
                txtApplyPeriodName.Text = dst.Tables[0].Rows[0]["ApplyPeriodName"].ToString();
                if (!string.IsNullOrEmpty(dst.Tables[0].Rows[0]["DateFrom"].ToString()))
                    txtDateFrom.Text = common.ChangeDateFormatToString(Convert.ToDateTime(dst.Tables[0].Rows[0]["DateFrom"].ToString()));
                if (!string.IsNullOrEmpty(dst.Tables[0].Rows[0]["DateTo"].ToString()))
                    txtDateTo.Text = common.ChangeDateFormatToString(Convert.ToDateTime(dst.Tables[0].Rows[0]["DateTo"].ToString()));
                drpArchivesStatus.SelectedValue = dst.Tables[0].Rows[0]["ArchivesStatus"].ToString();
                drpStatus.SelectedValue = dst.Tables[0].Rows[0]["Status"].ToString();
            }
        }
        protected void butSave_Click(object sender, EventArgs e)
        {
            if (PageType != null && PeriodID != null && PageType == "edit" && PeriodID.Trim().Length != 0)
            {
                PeriodClass.UpdatePeriod(PeriodID, common.changeDateFormatToDate(txtDateFrom.Text.Trim()).ToString(), txtApplyPeriodName.Text, common.changeDateFormatToDate(txtDateTo.Text.Trim()).ToString(), drpArchivesStatus.SelectedValue, user.UserID, drpStatus.SelectedValue);
            }
            else
            {
                PeriodClass.InsertPeriod(txtApplyPeriodID.Text, common.changeDateFormatToDate(txtDateFrom.Text.Trim()).ToString(), txtApplyPeriodName.Text, common.changeDateFormatToDate(txtDateTo.Text.Trim()).ToString(), drpArchivesStatus.SelectedValue, user.UserID, drpStatus.SelectedValue);
            }
        }
    }
}
